package top.starmc.starpet.listener

import org.bukkit.event.inventory.InventoryClickEvent
import taboolib.common.platform.event.SubscribeEvent
import taboolib.module.nms.getItemTag
import taboolib.module.nms.itemTagReader
import taboolib.platform.util.isNotAir
import top.starmc.starpet.config.Config
import top.starmc.starpet.util.updatePetData

object Inventories {
    @SubscribeEvent
    fun onClick(event: InventoryClickEvent) {
        val target = event.currentItem
        val item = event.cursor
        if (target.isNotAir() && item.isNotAir() && target.getItemTag().containsKey("宠物")) {
            val id = target.getItemTag()["宠物"]!!.asString()
            if (id in Config.internalMap) {
                Config.developList.firstOrNull { it.itemName in item.itemMeta.displayName }?.run {
                    target.itemTagReader {
                        val itemLevel = getInt("等级")!!
                        if (levelLimit == -1 || levelLimit > itemLevel) {
                            val experience = getInt("经验")!! + experience
                            val newLevel = levelTo + (itemLevel + level)
                            target.updatePetData(Config.internalMap[id]!!, experience, newLevel)
                            item.amount -= 1
                            event.isCancelled = true
                        }
                    }
                }
            }
        }
    }
}